package com.maximus.server.nio.multithreadingReactor;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class Processor implements Runnable {
    private static final Logger logger = LogManager.getLogger(Processor.class);

    private String line;
    private SocketChannel channel;

    public Processor(SocketChannel socketChannel, String line) {
        this.channel = socketChannel;
        this.line = line;
    }

    @Override
    public void run() {
        try {
            logger.info("客户端：" + channel.getRemoteAddress() + "发来消息：" + line);
            // 回显该行（自动补上换行符）
            channel.write(ByteBuffer.wrap((line + "\r\n").getBytes(StandardCharsets.UTF_8)));
        } catch (IOException e) {
            logger.error(e);
            throw new RuntimeException(e);
        }

    }
}
